<?php
/**
 * Created by PhpStorm.
 * User: zhaoyi
 * Date: 2022/1/26
 * Time: 11:46.
 */

declare(strict_types=1);

namespace App\Application\Api\Controller;

use App\Annotation\Api;
use App\Application\Admin\Model\Coupon;
use App\Application\Admin\Model\CouponMember;
use App\Application\Admin\Model\Order;
use App\Application\Admin\Model\Room;
use App\Application\Admin\Model\Store;
use App\Application\Admin\Service\AdminSettingService;
use App\Application\Admin\Service\CityService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Controller;

#[Controller("/api/coupon/")]
class CouponController extends AbstractController
{
    #[Inject]
    protected AdminSettingService $setting;

    #[Api]
    #[PostMapping("info")]
    function info()
    {
        $coupon_id = $this->request->input('id', '');

        $coupon = Coupon::where('coupon_id', $coupon_id)->select('*')
            ->first() ?: [];
        $roomOptions = Room::whereIn('room_id', $coupon['roomids'])->pluck('name')->toArray();
        $coupon['roomlist'] = implode(',', $roomOptions);


        return compact('coupon', 'roomOptions');
    }

    /*
     *
     *团购优惠卷
     * */
    #[Api]
    #[GetMapping("lists")]
    function lists()
    {
        $store_id = $this->request->input('store_id', '');
        $where = [];
        $where[] = ['store_id', '=', $store_id];
        $where[] = ['receive', '=', 0];
        $where[] = ['status', '=', 1];
        $lists = Coupon::where($where)
            ->select('*')
            ->get()->toArray();
        return compact('lists');
    }

    /*
     *
     *福利中心
     * */
    #[Api]
    #[PostMapping("index")]
    function index()
    {
        $store_id = $this->request->input('store_id', '');
        $where = [];
        $where[] = ['store_id', '=', $store_id];
        $where[] = ['receive', '=', 1];
        $where[] = ['status', '=', 1];
        $lists = Coupon::where($where)
            ->select('*')
            ->get()->toArray();
        $user = $this->checkToken();
        foreach ($lists as $key => $value) {
            $roomOptions = Room::whereIn('room_id', $value['roomids'])->pluck('name')->toArray();
            if (empty($roomOptions)) {
                $lists[$key]['roomlist'] = '所有包厢';
            } else {
                $lists[$key]['roomlist'] = implode(',', $roomOptions);;
            }
            //是否继续领取
            if ($value['user_num'] == 0) {
                $lists[$key]['user_num_status'] = 0;
            } else {
                //已领取数量
                $user_num = Db::table('coupon_member')->where('coupon_id', $value['coupon_id'])->where('member_id', $user->member_id)->count('*');
                $lists[$key]['user_num_status'] = $value['user_num'] - $user_num > 0 ? 0 : 1;
            }
        }
        return compact('lists');
    }

    /*
     *
     *领取
     * */
    #[Api]
    #[PostMapping("receive")]
    function receive()
    {
        $coupon_id = $this->request->input('coupon_id', '');
        $user = $this->checkToken();
        $where = [];
        $card_where['coupon_id'] = $coupon_id;
        $card_list = Coupon::where($card_where)->select('*')
            ->first() ?: [];;
        $dataCoupon = array(
            'coupon_id' => $card_list->coupon_id,
            'name' => $card_list->name,
            'condition' => $card_list->condition,
            'roomids' => json_encode($card_list->roomids),
            'time' => $card_list->time,
            'hours_add_time' => $card_list->hours_add_time,
            'hours_end_time' => $card_list->hours_end_time,
            'member_id' => $user->member_id,
            'store_id' => $card_list->store_id,
            'type' => $card_list->type,
            'money' => $card_list->money,
            'price' => $card_list->price,
            'end_time' => $card_list->end_time,
        );
        $res = Db::table('coupon_member')->insert($dataCoupon);
        if ($res) {
            // 更新领取数量
            $coi = array(
                'num' => $card_list['num'] - 1,
            );
            Coupon::where('coupon_id', $card_list->coupon_id)->update($coi);

        }
        return $res ? $this->returnSuccessJson(compact('res')) : $this->returnErrorJson();

    }


    /*
     *
     *我的优惠卷
     * */
    #[Api]
    #[PostMapping("userCoupon")]
    function userCoupon()
    {
        CouponMember::where('end_time', '<', date('Y-m-d H:i:s'))->update(['status' => 2]);
        $status = $this->request->input('status', '');
        $user = $this->checkToken();
        $where = [];
        $where[] = ['member_id', '=', $user->member_id];
        $where[] = ['status', '=', $status];
        $lists = CouponMember::where($where)
            ->select('*')
            ->get()->toArray();

        foreach ($lists as $key => $value) {
            $roomOptions = Room::whereIn('room_id', $value['roomids'])->pluck('name')->toArray();
            if (empty($roomOptions)) {
                $lists[$key]['roomlist'] = '所有包厢';
            } else {
                $lists[$key]['roomlist'] = implode(',', $roomOptions);;
            }

            $lists[$key]['store_name'] = Store::where('store_id', $value['store_id'])->value('store_name');
        }
        return compact('lists');
    }

    /*
 *
 *使用优惠卷
 * */
    #[Api]
    #[PostMapping("CouponOrder")]
    function CouponOrder()
    {
        CouponMember::where('end_time', '<', date('Y-m-d H:i:s'))->update(['status' => 2]);
        $status = $this->request->input('status', '');
        $order_on = $this->request->input('order_on', '');
        $order = Order::where('order_on', $order_on)->select('*')->first() ?: [];
        $user = $this->checkToken();
        $where = [];
        $where[] = ['member_id', '=', $user->member_id];
        $where[] = ['store_id', '=', $order->store_id];

        $where[] = ['status', '=', $status];

        $lists = CouponMember::where($where)->where(function ($query) use ($order) {
            $query->where('time', '>=', (int)$order->hour)->orWhere('time', '=', 0);
        })
            ->select('*')
            ->get()->toArray();

        foreach ($lists as $key => $value) {
            $roomOptions = Room::whereIn('room_id', $value['roomids'])->pluck('name')->toArray();
            if (empty($roomOptions)) {
                $lists[$key]['roomlist'] = '所有包厢';
            } else {
                $lists[$key]['roomlist'] = implode(',', $roomOptions);;
            }

            $lists[$key]['store_name'] = Store::where('store_id', $value['store_id'])->value('store_name');
        }
        return $lists ? $this->returnSuccessJson(compact('lists')) : $this->returnErrorJson();

    }
}